<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:55 
 * 类说明
 */
class UserAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$Model = M($this->getActionName());
		$map['flag'] = 1;
		$balance = $Model->where($map)->sum("balance");
		$this->assign('balance',$balance);
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		if(!empty($_GET['username'])){
			$map['username'] = array("like","%".$_GET['username']."%");
		}
		if(!empty($_GET['tel'])){
			$map['tel'] = array("like","%".$_GET['tel']."%");
		}
		return $map;
	}
	
	
	/**
	 * @authority 详细
	 */
	public function detailView() {
		$Model = M($this->getActionName());
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$referrer = $Model->where("id=".$vo['parent_id'])->field("username")->find();
		$vo['referrer'] = $referrer['username'];
		$this->assign('vo',$vo);
		$this->display();
	}
	
	//账户明细
	public function detail(){
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		$this->display();
	}
	
	//账户明细列表
	public function getAllDetailData(){
		$Model = M('UserDetails');
		$map['user_id'] = $_REQUEST['id'];
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	//查看地址
	public function address(){
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		$this->display();
	}
	
	//查看地址列表
	public function getAllAddressData(){
		$Model = M('Address');$SchoolModel = M('School');
		$map['user_id'] = $_REQUEST['id'];
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		for($i=0;$i<count($data);$i++){
			$schoolName = $SchoolModel->where("id=".$data[$i]['school_id'])->field("name")->find();
			if(isset($schoolName)){
				$data[$i]['school'] = $schoolName['name'];
			}else{
				$data[$i]['school'] = "无";
			}
		}
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	//重置密码
	public function resetPwd() {
		$Model = M($this->getActionName());
		$pk = $Model->getPk();
		$id = $_REQUEST[$pk];
		if (isset($id)) {
			$condition = array($pk => array('in', explode(',', $id)));
			if (false !== $Model->where($condition)->setField($_REQUEST['field'],$_REQUEST['islock'])) {
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}
	
	//修改手机号码
	public function modifyTel() {
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	//检查手机号码是否可用
	public function checkTel(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		$where['tel'] = $param;
		$count=$user->where($where)->count();
		if($count){
			echo '该手机号码已被使用！';
		}else{
			echo 'y';
		}
	}
	
	/**
	 * 更新
	 */
	public function update() {
		$Model = M($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$data['username'] = $data['tel'];
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	//图片传图
	public function uploadImg(){
		$folderName = $_POST['folderName'];//格式  首字母大写
		$prefix = $_POST['prefix'];//格式  m_,s_
		$maxWidth = $_POST['maxWidth'];//格式500,200
		$maxHeight = $_POST['maxHeight'];//格式500,200
		import('ORG.Net.UploadFile');
		$targetFolder_b = './Uploads/'.$folderName.'/'.date('Y-m-d').'/';
		if(!is_dir($targetFolder_b)){
			@mkdir($targetFolder_b,0777);
		}
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  $targetFolder_b;// 设置附件上传目录
		if($maxWidth != 0 && $maxHeight != 0){
			$upload->thumb = true;
			$upload->thumbPrefix = $prefix;  //缩略图前缀
			$upload->thumbMaxWidth = $maxWidth;
			$upload->thumbMaxHeight = $maxHeight;
			$upload->thumbType = 0;//按设置大小截取 0 按原图等比例缩略
			$upload->thumbRemoveOrigin = true; //上传后移除原图
		}
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->ajaxReturn(0,$upload->getErrorMsg(),0);
		}else{// 上传成功
			$info =  $upload->getUploadFileInfo();
			//补上缩略图的前缀
			if($maxWidth != 0 && $maxHeight != 0){
				$bak['url'] = ltrim($info[0]['savepath'],".").$prefix.$info[0]['savename'];
			}else{
				$bak['url'] = ltrim($info[0]['savepath'],".").$info[0]['savename'];
			}
			$bak['localname'] = $info[0]['name'];
			$this->ajaxReturn($bak,'上传成功！',1);
		}
	}
}